'use strict';
import React from "react";
import {ListView, Text, View, Image, TouchableOpacity, StyleSheet, RefreshControl} from "react-native";
import EventDetailsPage from "../Pages/EventDetailsPage";
import Global from "../Commom/GlobalConst";
import FoundAPIService from "../Commom/FoundAPIService";
import Toast from '../Utils/toast';
const styles = StyleSheet.create({
    container: {
        backgroundColor: 'white',
        padding: 10,
        flexDirection: 'row',
        alignItems: 'center',
        marginTop: 10,
    },
    image: {
        width: Global.Const.size.width * 3 / 10,
        height: 100,
        marginRight: 10,
    }
});

let startTime = 0;

class FoundPageTabActivityPage extends React.Component {

    constructor(props) {
        super(props);
        startTime = 0;
        this.state = {
            searchText: '',
            dataSource: new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}),
            isRefreshing: false,
            record_count: 1,
            page: 1,
            acitvityDataList: [],
        }
        this.ActivitySearch = this.ActivitySearch.bind(this);
    }

    componentDidMount() {
        FoundAPIService.activityList(this.state.searchText, 1).then((ret)=> {
            this.setState({
                record_count: ret.record_count,
                page: Number(ret.page),
                acitvityDataList: ret.result,
                dataSource: this.state.dataSource.cloneWithRows(ret.result),
            });
        }).catch((err)=> {

        });
    }

    onRefresh() {
        this.setState({
            isRefreshing: true,
        });
        FoundAPIService.activityList(this.state.searchText, 1).then((ret)=> {
            this.setState({
                isRefreshing: false,
                record_count: ret.record_count,
                page: Number(ret.page),
                acitvityDataList: ret.result,
                dataSource: this.state.dataSource.cloneWithRows(ret.result),
            });
            Toast.show('刷新成功');
        }).catch((err)=> {

        });
    }

    loadMoreData() {
        if (this.state.page < this.state.record_count) {
            this.setState({
                isRefreshing: true,
            });
            if (searchText.length > 0) {
                this.setState({
                    searchText: searchText
                });
                FoundAPIService.activityList(searchText, this.state.page + 1).then((ret)=> {
                    var wanjian = [];
                    wanjian = this.state.travelDataList;
                    for (var i = 0; i < ret.result.length; i++) {
                        wanjian.push(ret.result[i])
                    }
                    this.setState({
                        isRefreshing: false,
                        record_count: ret.record_count,
                        page: Number(ret.page),
                        acitvityDataList: wanjian,
                        dataSource: this.state.dataSource.cloneWithRows(wanjian),
                    });
                }).catch((err)=> {

                });
            } else {
                FoundAPIService.activityList('', this.state.page + 1).then((ret)=> {
                    var wanjian = [];
                    wanjian = this.state.travelDataList;
                    for (var i = 0; i < ret.result.length; i++) {
                        wanjian.push(ret.result[i])
                    }
                    this.setState({
                        isRefreshing: false,
                        record_count: ret.record_count,
                        page: Number(ret.page),
                        acitvityDataList: wanjian,
                        dataSource: this.state.dataSource.cloneWithRows(wanjian),
                        searchText: ''
                    });
                }).catch((err)=> {

                });
            }
        } else {
            if (this.state.page != 1) {
                Toast.show('木有更多数据');
            }
        }
    }

    ActivitySearch(searchText) {
        this.setState({
            isRefreshing: true,
        });
        if (searchText.length > 0) {
            this.setState({
                searchText: searchText
            });
            FoundAPIService.activityList(searchText, 1).then((ret)=> {
                this.setState({
                    isRefreshing: false,
                    record_count: ret.record_count,
                    page: Number(ret.page),
                    acitvityDataList: ret.result,
                    dataSource: this.state.dataSource.cloneWithRows(ret.result),
                });
            }).catch((err)=> {
                this.setState({
                    isRefreshing: false,
                    record_count:1,
                    page:1,
                    acitvityDataList:[],
                    dataSource: this.state.dataSource.cloneWithRows([]),
                });
            });
        } else {
            FoundAPIService.activityList('', this.state.page).then((ret)=> {
                this.setState({
                    isRefreshing: false,
                    record_count: ret.record_count,
                    page: Number(ret.page),
                    acitvityDataList: ret.result,
                    dataSource: this.state.dataSource.cloneWithRows(ret.result),
                    searchText: ''
                });
            }).catch((err)=> {
                this.setState({
                    isRefreshing: false,
                    record_count:1,
                    page:1,
                    acitvityDataList:[],
                    dataSource: this.state.dataSource.cloneWithRows([]),
                });
            });
        }
    }

    _renderRow(row) {
        return (
            <TouchableOpacity onPress={() => {
                this.props.navigator.push({
                    name: 'EventDetailsPage',
                    component: EventDetailsPage,
                    passProps: {
                        ID: row.id ,
                    }
                })
            }}>
                <View style={styles.container}>
                    <Image style={styles.image} source={{uri:row.images}}/>
                    <View style={{flex: 1, alignItems: 'flex-start', height: 100}}>
                        <Text numberOfLines={2}
                              style={{
                                  fontSize: 16,
                                  color: Global.FontColor.DimGray,
                                  lineHeight: 20,
                                  height: 45
                              }}>{row.title}</Text>
                        <View style={{flexDirection: 'row', alignItems: 'center', marginTop: 5}}>
                            <Image style={{
                                width: 14,
                                height: 14,
                                marginRight: 5,
                                resizeMode: 'contain',
                            }}
                                   source={require('../../Image/icon_gray_position.png')}/>
                            <Text numberOfLines={1}
                                  style={{
                                      fontSize: 13,
                                      color: Global.FontColor.LightGrey
                                  }}>{row.address}</Text>
                        </View>
                        <View style={{
                            flexDirection: 'row',
                            alignItems: 'center',
                            marginTop: 5
                        }}>
                            <Image style={{
                                width: 12,
                                height: 12,
                                marginRight: 5,
                                resizeMode: 'contain',
                            }}
                                   source={require('../../Image/icon_gray_time.png')}/>
                            <Text numberOfLines={1}
                                  style={{
                                      fontSize: 13,
                                      color: Global.FontColor.LightGrey
                                  }}>{row.start_time}</Text>
                        </View>
                    </View>
                </View>
            </TouchableOpacity>
        )
    }

    render() {
        return (
            <ListView
                dataSource={this.state.dataSource}
                renderRow={(row) => this._renderRow(row)}
                style={{backgroundColor: '#F5F5F5'}}
                onEndReached={() => this.loadMoreData()}
                onEndReachedThreshold={100}
                enableEmptySections={true}
                initialListSize={6}
                refreshControl={
                    <RefreshControl
                        colors={['#4CAF50', '#4CAF50', '#4CAF50', '#4CAF50']}
                        refreshing={this.state.isRefreshing}
                        onRefresh={()=> {
                            this.onRefresh();
                        }}
                    />
                }
            />
        )
    }

}

export default FoundPageTabActivityPage;